package com.ruoyi.socket.handler;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.gams.domain.*;
import com.ruoyi.gams.mapper.*;
import com.ruoyi.socket.DeviceClientInfoDO;
import com.ruoyi.socket.server.SocketClientStore;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Slf4j
public class MessageHandler
{

    @Autowired
    private GamsDeviceMapper deviceMapper;

    @Autowired
    private CallDeviceMessageHandler callDeviceMessageHandler;

    @Autowired
    private SocketClientStore socketClientStore;

    @Autowired
    private GamsWindowMapper windowMapper;

    @Autowired
    private GamsStaffMapper staffMapper;

    @Autowired
    private GamsScoringMapper scoringMapper;

    @Autowired
    private GamsUserQueueMapper userQueueMapper;

    public void getResultMsg(DeviceClientInfoDO clientParam, ChannelHandlerContext ctx)
    {
        String msg = clientParam.getMessage();
        String clientKey = ctx.channel().id().toString();
        log.debug("key = " + clientKey + ",msg = " + msg);
        log.info("key = " + clientKey + ",msg = " + msg);
        if (msg.contains("HTTP/1.1"))
        {
            log.info("消息不完整:");
            socketClientStore.send(ctx, "success");
            return;
        }
        if (!msg.startsWith("{") || !msg.endsWith("}"))
        {
            socketClientStore.send(ctx, msg);
            return;
        }

        JSONObject object = JSONObject.parseObject(msg);
        String command = object.getString("c");
        String deviceId = object.getString("d");
        String m = object.getString("m");

        switch (command)
        {
            //联网
            case "1":
            {
                GamsDevice device = deviceMapper.findFirstByDeviceId(deviceId);
                //找不到设备，添加默认
                if (device == null)
                {
                    GamsDevice record = new GamsDevice();
                    record.setClientKey(clientKey);
                    record.setCreateTime(new Date());
                    record.setDeviceId(deviceId);
                    record.setRemark(m);
                    deviceMapper.insertGamsDevice(record);
                    return;
                }

                deviceMapper.updateClientKeyByDeviceId(clientKey, deviceId);
                deviceMapper.updateHeartTimeByClientKey(new Date(), clientKey);
                socketClientStore.sendHeart(ctx);

                break;
            }
            //心跳
            case "2":
                deviceMapper.updateHeartTimeByClientKey(new Date(), clientKey);
                socketClientStore.sendHeart(ctx);
                break;
            //呼叫器 demo FF6803036800010C0D16
            case "4":
                //呼叫器消息解析
                callDeviceMessageHandler.getResultMsg(m);
                break;
            //评价器
            case "5":
            {
                GamsDevice device = deviceMapper.findFirstByDeviceId(deviceId);
                if (device == null)
                {
                    log.info("device is null");
                    return;
                }

                GamsWindow window = windowMapper.findFirstByScoringDeviceId(device.getId());
                if (window == null)
                {
                    log.info("window is null");
                    return;
                }

                GamsStaff staff = staffMapper.selectGamsStaffById(window.getStaffId());
                if (staff == null)
                {
                    log.info("staff is null");
                    return;
                }
                //1.手头的号状态改成完成
                GamsUserQueue queue = userQueueMapper.findFirstByStaffIdAndStatus(window.getStaffId(), 2);
                if (queue == null)
                {
                    log.info("queue is null");
                    return;
                }
                //2.修改排队第一的号
                String queueIds = window.getBusinessQueueIds();
                String[] ids = queueIds.split(",");
                //2.修改排队第一的号
                GamsUserQueue userQueue = userQueueMapper.findFirstByBussinessIdAndStatus(Integer.valueOf(ids[0]), 1);

                JSONObject data = object.getJSONObject("data");
                Integer attitude = data.getInteger("attitude");
                Integer quality = data.getInteger("quality");
                Integer efficiency = data.getInteger("efficiency");
                Integer autonomy = data.getInteger("autonomy");

                Integer count = scoringMapper.countByQueueId(queue.getId());
                if (count > 0)
                {
                    log.info("已经评价过了");
                    return;
                }

                //插入评价记录
                GamsScoring record = new GamsScoring();
                record.setAttitude(attitude);
                record.setAutonomy(autonomy);
                record.setEfficiency(efficiency);
                record.setQuality(quality);
                record.setBussinessId(userQueue.getBussinessId());
                record.setBussinessName(userQueue.getBussinessName());
                record.setQueueId(queue.getId());
                record.setCreateTime(new Date());
                record.setStaffId(staff.getId());
                record.setStaffName(staff.getStaffName());
                scoringMapper.insertGamsScoring(record);
                break;
            }
            //综合屏
            case "6":
                break;

        }
    }
}
